package com.ht.shiro;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.dto.User;
import com.ht.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


import javax.annotation.Resource;

public class LoginRealm extends AuthorizingRealm {


    @Resource
    private UserMapper userMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("进入权限方法");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("进入登录方法");
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username = token.getUsername();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //根据username查询
        queryWrapper.eq("deleted", 0).eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            return null;
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),user.getUsername());
    }
}